package com.etc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.AnnouncementDao;
import com.etc.entity.Announcement;
import com.etc.util.DBUtil;

public class AnnouncementDaoImpl implements AnnouncementDao{

	@Override
	public List<Announcement> queryAllAnnouncement() {
		String sql="SELECT * FROM announcement";
		CachedRowSet set = DBUtil.execQuery(sql);
		Announcement announcement=null;
		List<Announcement> list =new ArrayList<Announcement>();
		try {
			while (set.next()) {
				int a_id = set.getInt(1);
				String a_user = set.getString(2);
				String a_title = set.getString(3);
                String a_content = set.getString(4);
                String a_time = set.getString(5);
                 announcement = new Announcement(a_id, a_user, a_title, a_content, a_time);
                 list.add(announcement);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean addAnnouncement(Announcement announcement) {
		String sql="INSERT INTO announcement VALUES(null,?,?,?,NOW())";
		boolean b = DBUtil.executeUpdate(sql, announcement.getA_user(),announcement.getA_title(),announcement.getA_content());
		return b;
	}

	@Override
	public boolean rmvAnnouncement(int id) {
		String sql="DELETE FROM announcement WHERE a_id=?";
		boolean b = DBUtil.executeUpdate(sql, id);
		return b;
	}

	@Override
	public Announcement queryAnnouncement(String name) {
		String sql="SELECT * FROM announcement where a_content=?";
		CachedRowSet set = DBUtil.execQuery(sql,name);
		Announcement announcement=null;
		try {
			while (set.next()) {
				int a_id = set.getInt(1);
				String a_user = set.getString(2);
				String a_title = set.getString(3);
                String a_content = set.getString(4);
                String a_time = set.getString(5);
                 announcement = new Announcement(a_id, a_user, a_title, a_content, a_time);
       
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return announcement;
	}

}
